package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * @author LinYiXiong
 * @version 1.0.0
 * @description TODO
 * @date 2022/6/18 9:50
 */
@RestController
public class CartController {

    @Autowired
    ICartService iCartService;

    @PostMapping("shopping/carts")
    public ResponseData addProductToCart(@RequestBody AddCartRequest request) {

        AddCartResponse response = iCartService.addToCart(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil<>().setData(null);
        }
        // 调用失败
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @GetMapping("shopping/carts")
    public ResponseData getAllProductOfCart(HttpServletRequest request) {
        String userInfo = (String) request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long userId = Long.parseLong(object.get("uid").toString());
        //Long userId = Long.valueOf(74);
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(userId);
        CartListByIdResponse response = iCartService.getCartListById(cartListByIdRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil<>().setData(response.getCartProductDtos());
        }
        // 调用失败
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @PutMapping("shopping/carts")
    public ResponseData updateProductOfCart(@RequestBody UpdateCartNumRequest request) {
        UpdateCartNumResponse response = iCartService.updateCartNum(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil<>().setData(null);
        }
        // 调用失败
        return new ResponseUtil<>().setErrorMsg(response.getMsg());

    }
    //删除购物车中选中的商品
    @DeleteMapping("shopping/items/{uid}")
    public ResponseData DeleteCheckedItem(@PathVariable Long uid) {
        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(uid);

        DeleteCheckedItemResposne response = iCartService.deleteCheckedItem(request);

        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    //删除购物车中的商品
    @DeleteMapping("shopping/carts/{uid}/{pid}")
    public ResponseData deleteCartItem(@PathVariable Long uid, @PathVariable Long pid) {
        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setUserId(uid);
        request.setItemId(pid);

        DeleteCartItemResponse response = iCartService.deleteCartItem(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());

    }

    //全选
    @PutMapping("shopping/items")
    public ResponseData CheckAllItem(@RequestBody CheckAllItemRequest request) {
        CheckAllItemResponse response = iCartService.checkAllCartItem(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}
